<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="baidu-site-verification" content="code-UjJKh3jtOo">
  <link rel="stylesheet" href="//unpkg.com/@highlightjs/cdn-assets@11.5.0/styles/atom-one-dark.min.css">
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  <title>Tit1e</title>
  <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2767111597631391" crossorigin="anonymous"></script>
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="Tit1e" type="application/atom+xml">
</head>

<body class="dark">
    <div class="container">
    <header class="header">
  <div class="title">
      <a href="/" class="logo">Tit1e</a>
      <div class="btn-dark"></div>
      <script>
        let bodyClx = document.body.classList;
        let btnDark = document.querySelector('.btn-dark');
        let sysDark = window.matchMedia('(prefers-color-scheme: dark)');
        let darkVal = localStorage.getItem('dark');

        let setDark = (isDark) => {
        bodyClx[isDark ? 'add' : 'remove']('dark');
        localStorage.setItem('dark', isDark ? 'yes' : 'no');
        };

        setDark(darkVal ? darkVal === 'yes' : sysDark.matches);
        requestAnimationFrame(() => bodyClx.remove('not-ready'));

        btnDark.addEventListener('click', () => setDark(!bodyClx.contains('dark')));
        sysDark.addEventListener('change', (event) => setDark(event.matches));
      </script>
  </div>
    <ul class="menu">
        
        
        
        
        <li class="menu-item ">
            <a href="/" class="menu-item-link">Home</a>
        </li>
        
        
        <li class="menu-item ">
            <a href="/archives/" class="menu-item-link">Archives</a>
        </li>
        
        
        <li class="menu-item ">
            <a href="/atom.xml" class="menu-item-link">RSS</a>
        </li>
        
        
        <li class="menu-item ">
            <a target="_blank" rel="noopener" href="https://pixeltimer.art" class="menu-item-link">Pixel Timer</a>
        </li>
        
    </ul>
    <div class="search-box">
        <input type="search" id="search" placeholder="搜索">
    </div>
</header>
    <article class="post">
  <div class="post-meta">
    <span class="post-time">4 / 27, 2020</span>
  </div>
  <div class="post-title">
    <h1 class="title">uni-app 小程序设置自定义字体</h1>
  </div>
  
  <ul class="post-tags">
    
    <li class="tag-item">
      #小程序
    </li>
    
  </ul>
  
  <div class="post-content">
    <p>最近在开发动森的一款小程序，到目前为止踩了不少坑，今天记录一下小程序引入自定义字体的方式。后续会陆续更新其他踩坑记录。</p>
<p>首先，小程序是支持使用自定义字体的，使用 <a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/api/ui/font/wx.loadFontFace.html">wx.loadFontFace()</a> 就可以引入字体，但是使用这个 api 有一些限制：</p>
<p><img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-143333.png"></p>
<p>需要特别注意的点有：</p>
<p>第一点：字体格式，保险起见使用 ttf 格式，如果不是 ttf 格式，可以在 <a target="_blank" rel="noopener" href="https://transfonter.org/">https://transfonter.org/</a> 在线转一下。</p>
<p>第二点：字体链接必须是 <strong>https</strong>，意味着不支持引入本地字体。</p>
<p>第三点：链接必须<strong>同源</strong>或者开启了 <strong>cors</strong> 支持。</p>
<p>其他几点注意点对使用有影响，但没以上三点重要。</p>
<p>我将字体上传到了腾讯的对象存储上，因此我要在存储桶的设置中对 servicewechat.com 开启 cors 支持：</p>
<p>1、登录腾讯云，进入对象存储控制台</p>
<p>2、点击<strong>【存储桶列表】</strong>菜单</p>
<p><img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-144352.png"></p>
<p>3、点击上传了字体的存储桶，进入存储桶详情</p>
<p><img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-144625.png"></p>
<p>4、点击左侧<strong>【基础配置】</strong>菜单</p>
<p><img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-144749.png"></p>
<p>5、找到<strong>【跨域访问CORS设置】</strong>卡片，点击添加规则，照下图填写表单并保存，过一会儿就会生效。</p>
<p><img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-144838.png"></p>
<p>官方文档上说如果需要全局支持字体的话 <code>wx.loadFontFace</code> 需要在 <code>app.js</code> 中调用并且设置 <code>global</code> 为 <code>true</code>。</p>
<p>并且使用 <code>global</code> 属性对调试基础库版本有要求，需要 2.10.0 以上，否则字体只会在调用这个 api 的页面生效。<img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-150114.png"></p>
<p>还有注意中提到的第四点也注意一下，可能会有个报错，但是字体实际已经生效，这个报错可以忽略。</p>
<p>但是我使用的是 <code>uni-app</code>，我尝试写在 <code>main.js</code> 入口文件中，但是并没有生效，不过 <code>uni-app</code> 支持 vue 的 mixins，创建一个font.js：</p>
<pre><code class="javascript">// @/mixins/font.js

export default &#123;
    onLoad() &#123;
        uni.loadFontFace(&#123;
            family: &#39;DFYuanW9&#39;,
            source: &#39;url(&quot;https://xxxxxxx.ttf&quot;)&#39;,
        &#125;)
    &#125;
&#125;
</code></pre>
<p>在 <code>main.js</code> 中：</p>
<pre><code class="javascript">import mixin from &#39;@/mixins/font.js&#39;

Vue.mixin(mixin)
</code></pre>
<p>并在全局 css 中使用字体：</p>
<pre><code class="css">page&#123;
  font-family: &#39;DFYuanW9&#39;
&#125;
</code></pre>
<p>这样每个页面就都会生效了。</p>
<p>下面是字体使用效果：</p>
<p>不使用字体：</p>
<p><img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-IMG_2437.PNG"></p>
<p>使用字体：</p>
<p><img src="https://personal-1251959693.cos.ap-chengdu.myqcloud.com/2020-04-27-IMG_2436.PNG"></p>

  </div>
  <div class="post-near">
    
    <a
      class="post-near-prev"
      href="/p/9623f65.html"
      title="uni-app 小程序给所有页面添加分享"
    >
      <span class="arrow">←</span>
      <span class="post-near-title">
        uni-app 小程序给所有页面添加分享
      </span>
    </a>
    
    
    <a
      class="post-near-next"
      href="/p/cac2df04.html"
      title="element-ui el-table 表格高度自适应问题解决方案"
    >
      <span class="post-near-title">
        element-ui el-table 表格高度自适应问题解决方案
      </span>
      <span class="arrow">→</span>
    </a>
    
  </div>
</article>

    <footer class="footer">
  <p>本博客采用 <a class="license-link" rel="license noopener" target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">署名-非商业性使用-相同方式共享 4.0 国际</a>协议进行许可</p>
  <p>
    <span>Thanks For <a target="_blank" rel="noopener" href="https://github.com/nanxiaobei/hugo-paper" taget="_blank">hugo-paper</a></span>
    <span style="margin-left: 10px;">&copy; 2016-2025 by Tit1e</span>
  </p>
</footer>
    </div>
    <script src="//unpkg.com/@highlightjs/cdn-assets@11.5.0/highlight.min.js"></script>
<script>
  hljs.initHighlightingOnLoad();
  var _hmt = _hmt || [];
    (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?6349d3bc54baea0b04a65145c5e3b799";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
    })();
    const block = document.querySelectorAll('.post-content')[0]
    block.addEventListener('click', e => {
        const {nodeName, src} = e.target
        if(nodeName !== 'IMG') return
        document.body.classList.add('overflow-hidden')
        const div = document.createElement('div')
        div.id = 'global-cover'
        div.title = '点击关闭'
        const a = document.createElement('a')
        a.href = src
        a.target="_blank"
        a.title = '点击打开原图'
        const img = document.createElement('img')
        img.className = 'show-pic'
        img.src = src
        a.appendChild(img)
        div.appendChild(a)
        document.body.appendChild(div)
    })
    document.body.addEventListener('click', e => {
        if(e.target.id === 'global-cover'){
            document.body.classList.remove('overflow-hidden')
            document.body.removeChild(e.target)
        }
    })
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-7M04VN3JZ4"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-7M04VN3JZ4');
</script>

<script src="/js/search.js"></script>


</body>
</html>